package com.t282.crm.controller;

import com.t282.crm.entity.SysRight;
import com.t282.crm.entity.SysRole;
import com.t282.crm.entity.SysUser;
import com.t282.crm.service.SysRoleService;
import com.t282.crm.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private SysUserService userService;

    @ApiOperation("用户登录")
    @RequestMapping(value = "/doLogin")
    public String dologin(String usrName, String usrPassword, HttpServletRequest request) {
        SysUser user = userService.getSysUserByUsrNameAndUsrPassword(usrName, usrPassword);

        if (user != null) {
            request.getSession().setAttribute("loginUser", user);
            return "/body/main";
        } else {
            request.setAttribute("msg", "登录失败，用户名或密码错误！");
            return "login";
        }

    }

    @RequestMapping(value = "logOut")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        return "redirect:/user/login";   //建议重定向，保证删除Cookie
    }

    @RequestMapping("login")
    public String login(){
        return "login";
    }

}
